package com.tdam2011.grupo02.ui;

import java.util.ArrayList;

import com.tdam2011.grupo02.R;

import android.database.Cursor;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.provider.ContactsContract;

public class ContactosPreferenceActivity extends PreferenceActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.contactos_preference);
		
		Cursor grupos = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null,
				null, null);

		
		
		ArrayList<String> groups=new ArrayList<String>();
		if(grupos.moveToFirst())
		groups.add(grupos.getString(grupos.getColumnIndex(ContactsContract.Groups.ACCOUNT_NAME)));
		while(grupos.moveToNext()){
			if(!groups.contains(grupos.getString(grupos.getColumnIndex(ContactsContract.Groups.ACCOUNT_NAME))))
				groups.add(grupos.getString(grupos.getColumnIndex(ContactsContract.Groups.ACCOUNT_NAME)));
		}
		CharSequence[] entries=new CharSequence[groups.size()+2];
		String[] entryValues=new String[groups.size()+2];
		entries[0]=getString(R.string.opcionContactosFTipoTodos);
		entries[1]=getString(R.string.opcionContactosFTipoTel);
		entryValues[0]="0";
		entryValues[1]="1";
		int i=2;
		for(String r:groups){
			entries[i]=r;
			entryValues[i]=r;
			i++;
		}
		
		ListPreference listPref = new ListPreference(this);
        listPref.setKey(getString(R.string.preferenceContactosFTipo));
        listPref.setEntries(entries);
        listPref.setEntryValues(entryValues);
        listPref.setTitle(getString(R.string.preferenceContactosFTipoTitle));
        listPref.setSummary(getString(R.string.preferenceContactosFTipoSummary));
        listPref.setDefaultValue("0");
        PreferenceCategory category=(PreferenceCategory)this.getPreferenceScreen().findPreference(getString(R.string.preferenceContactosF));
		category.addPreference(listPref);
	}
}
